package Util;

import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.JTableHeader;

import redis.clients.jedis.Connection;

public class jFrame2 extends JFrame {
	public 
	JPanel bookmainpanel;
	JPanel booksearch;
	JPanel bookin2;
	JPanel bookamount;
	JPanel bookin;
	JPanel bookchange;
	JPanel bookdetele;
	JPanel bookdetele2;
	JLabel label1;
	JButton bookButton1;
	JButton bookButton2;
	JButton bookButton3;
	JButton bookButton4;
	JButton bookButton5;
	JButton bookButton6;
	JButton bookButton7;
	JTextField inField1;
	JTextField inField2;
	JTextField inField3;
	JTextField inField4;
	JTextField inField5;
	JTextField inField6;
	JTextField inField7;
	JTextField inField8;
	JTextField inField9;
	JTextField inField10;
	JTextField inField11;
	JTextField inField12;
	JTextField inField13;
	JTextField inField14;


	public jFrame2(){
		LayoutManager  bookview = new GridLayout(7,1);
        setLayout(bookview);
        setTitle("书店销售系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400, 400, 485, 343);
		setLocationRelativeTo(null);
		bookmainpanel= new JPanel(new GridLayout(7,1));
		booksearch = new JPanel();
		bookin2 = new JPanel();
		bookamount = new JPanel();
		bookchange = new JPanel();
		bookin = new JPanel();
		bookdetele= new JPanel();
		
		bookdetele2= new JPanel();
		bookButton1 = new JButton("库存");
		 
		bookButton1.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){
					 
	                java.sql.Statement st = conn.createStatement();
	                  String a=null;
					String sql="select * from BOOK ";
	                ResultSet rs=st.executeQuery(sql); 
	                while(rs.next()) {
	    				String bnum =rs.getString("bnum");
	    				String  bname=rs.getString("bname");
	    				String writer =rs.getString("writer");
	    				String press = rs.getString("press");
	    				String kind =rs.getString("kind");
	    				String price =rs.getString("price");
	    				String amount =rs.getString("amount");
	    	 
	    			
	    			 
	    				 a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price+"amount："+amount;
	    				  System.out.println(a); 
	    				  result_interface result_interface = new result_interface(a.toString());
	    			}
	                
	               
	                JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                
	       
	           
	             
	             



	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
				
			}
		
		});
		bookButton2 = new JButton("查询");
		bookButton2.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
			
				// TODO Auto-generated method stub
				new searchjFrame();
			}
			
		});
		bookButton3 = new JButton("入库");
		label1=new JLabel("先输入数量在输入书籍号");
		bookButton3.addActionListener( new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                PreparedStatement ps = null;
	                String newamount=inField9.getText();
	                String bnum2=inField10.getText();
	                String sql= "update book set amount =amount +? where bnum =? ";
	              
	               ps=conn.prepareStatement(sql);  
	                
	                ps.setString(1, newamount);
	                ps.setString(2, bnum2);
	                ResultSet rs=ps.executeQuery();
	              
	                JOptionPane.showMessageDialog(null,"修改成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);

	                
	                
	                
	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
				
			
				
			}
			
		});
		bookButton4=new JButton("添加");
	 
		bookButton4.addActionListener( new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                PreparedStatement ps = null;
	                String bnum =  inField1.getText();
	                String bname= inField2.getText();
	                String writer= inField3.getText();
	                String press = inField4.getText();
	                String kind =inField5.getText();
	                String price =inField6.getText();
	                String iswelcome=inField7.getText();
	                String amount =inField8.getText();
	                String sql= "insert into book values(?,?,?,?,?,?,?,?)";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, bnum);
	                ps.setString(2, bname);
	                ps.setString(3, writer);
	                ps.setString(4, press);
	                ps.setString(5, kind);
	                ps.setString(6, price);
	                ps.setString(7, iswelcome);
	                ps.setString(8, amount);
	                ResultSet rs=ps.executeQuery();
	                JOptionPane.showMessageDialog(null,"添加成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);

	                
	                
	                
	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
				
			}
			});
		bookButton5=new JButton("改价格");
		bookButton5.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                PreparedStatement ps = null;
	                String condition=inField11.getText();
	                String obj=inField12.getText();
	                String sql= "update book set price =? where bnum =?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, condition);
	                ps.setString(2,obj);
	               
	                ResultSet rs=ps.executeQuery();
	                JOptionPane.showMessageDialog(null,"改价成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);

	                
	                
	                
	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
			}
			});
	bookButton6=new JButton("按书号删除");
	bookButton6.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			try (java.sql.Connection conn = Myutil.getConnection()){

                java.sql.Statement st = conn.createStatement();
                PreparedStatement ps = null;
                String bnum	 =  inField13.getText();
                if(inField13.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"书号不能为空","提示",
                            JOptionPane.WARNING_MESSAGE);
                }
                else if(!inField13.getText().equals("")){
                String sql= "delete from book where  bnum=?";
                ps=conn.prepareStatement(sql);
                ps.setString(1, bnum);
                ResultSet rs=ps.executeQuery();
                JOptionPane.showMessageDialog(null,"删除成功","成功",
                        JOptionPane.INFORMATION_MESSAGE);
                }
                
                
                
                
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
		}
		
	});
	bookButton7=new JButton("按书名删除");
	bookButton7.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			try (java.sql.Connection conn = Myutil.getConnection()){

                java.sql.Statement st = conn.createStatement();
                PreparedStatement ps = null;
                String bname	 =  inField14.getText();
                if(inField13.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"书名不能为空","提示",
                            JOptionPane.WARNING_MESSAGE);
                }
                else if(!inField13.getText().equals("")){
                String sql= "delete from book where  bname=?";
                ps=conn.prepareStatement(sql);
                ps.setString(1, bname);
                ResultSet rs=ps.executeQuery();
                JOptionPane.showMessageDialog(null,"删除成功","成功",
                        JOptionPane.INFORMATION_MESSAGE);
                }
                
                
                
                
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
		}
		
	});
		
			
		
		
		inField1=new JTextField(8);
		inField2=new JTextField(8);
		inField3=new JTextField(8);
		inField4=new JTextField(8);
		inField5=new JTextField(8);
		inField6=new JTextField(8);
		inField7=new JTextField(8);
		inField8=new JTextField(8);
		inField9 = new JTextField(8);
		inField10 = new JTextField(8);
		inField11= new JTextField(8);
		inField12 = new JTextField(8);
		inField13 = new JTextField(8);
		inField14 = new JTextField(8);
		bookamount.add(bookButton1);
	
		
		booksearch.add(bookButton2);
		bookin2.add(bookButton3);
		bookin2.add(label1);
	 	bookin.add(bookButton4);
		bookchange.add(bookButton5);
		bookdetele.add(bookButton6);
		bookdetele2.add(bookButton7);
		bookin.add(inField1);
		bookin.add(inField2);
		bookin.add(inField3);
		bookin.add(inField4);
		bookin.add(inField5);
		bookin.add(inField6);
		bookin.add(inField7);
		bookin.add(inField8);
		bookin2.add(inField9);
		bookin2.add(inField10);
		bookchange.add(inField11);
		bookchange.add(inField12);
		bookdetele.add(inField13);
		bookdetele2.add(inField14);
		add(bookamount);
		add(booksearch);
		add(bookin2);
		add(bookin);
		add(bookchange);
		add(bookdetele);
		add(bookdetele2);
		setVisible(true);
		 
		
		
		
		
		
		
		
		
	}

}
